//
//  ST_ConversationSummarryModel.swift
//  btvideo
//
//  Created by mac on 2020/7/17.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import NIMSDK
import SwiftyJSON
import SwiftyAttributes
//import DifferenceKit




/**
 后台接口提供 可缓存
 */
struct ST_ConversationSummarryCacheStruct:Codable {
    
    var sessionId = ""
    
    var headPic = ""
    
    var userName = ""
    /// 昵称备注
    var remark:String
    /// 女神
    var isGoddess:Bool
    
    var face_audit_state = 0
    
    var isVip = false
    
    var nobilityImg:String?
    
    /**
     守护时间
     */
    var guardTime = 0
    
    var guardContent = ""
    
    /**
     守护置顶
     */
    var guardTop = false
    /**
     1:碰碰，2:闪聊，3:推荐
     */
    var source = 0
    
    
    // 亲密度 后台字段intimacy_image
    var sweetValueImage:String?
    
    var online_state = ""
    
}



fileprivate var normal_attributed:[Attribute] = [.font(.systemFont(ofSize: 14, weight: UIFont.Weight.medium)),.textColor(UIColor(red: 0.38, green: 0.4, blue: 0.44, alpha: 1))]


fileprivate var high_line_attributed:[Attribute] = [.font(.systemFont(ofSize: 14, weight: UIFont.Weight.medium)),.textColor(#colorLiteral(red: 0.9215686275, green: 0.2352941176, blue: 0.2352941176, alpha: 1))]


/// 聊天最近会话
class ST_TianSessionSummaryStruct {
    
    let sessionId:String
    let lastMessage:NSAttributedString
    let lastMsgSent:Bool
    let timeString:String
    let date:Double
    
    ///守护影响Cell排序
    var guardTop = false
    
    var unReadCount:Int = 0
    
    init(sessionId: String, lastMessage: NIMMessage, unReadCount: Int, timeString: String, date: Double) {
        
        self.sessionId = sessionId
        
        self.unReadCount = unReadCount
        self.timeString = timeString
        self.date = date
        
        
        lastMsgSent = lastMessage.isOutgoingMsg
        
        switch lastMessage.messageType {
        
        case .text where lastMessage.text?.count ?? 0 > 0:
            
            self.lastMessage =   (lastMessage.text ?? "").withAttributes(normal_attributed)
            
        case .image:
            
            self.lastMessage = "[图片]".withAttributes(normal_attributed)
        case .video:
            
            self.lastMessage = "[视频]".withAttributes(normal_attributed)
        case .audio:
            
            self.lastMessage = "[语音]".withAttributes(normal_attributed)
        case .custom:
            
            
            guard let attachment = (lastMessage.messageObject as? NIMCustomObject)?.attachment else{
                
                self.lastMessage =   "[不支持的消息类型，请更新最新版后查看]".withAttributes(normal_attributed)
                return
            }
             
            switch attachment {
            
            case _ as ST_BTIMInviteMessageModel,_ as ST_BTFemaleInviteChatMessageModel:
                
                self.lastMessage =   "[邀请类消息]".withAttributes(normal_attributed)
                
            case _ as ST_Custom264AttachmentModel:
                
                self.lastMessage =   "[提示类消息]".withAttributes(normal_attributed)
                
            case let cm as ST_Custom2649AttachmentModel:
                
                self.lastMessage =   cm.wechatText.withAttributes(normal_attributed)
                
                
            case let giftModel as ST_BTIMGiftMessageModel:
                
                /*
                 发送：
                 送礼物：你赠送了一个礼物
                 送守护：你赠送了一个守护
                 送会员：你赠送了会员
                 收到：
                 收礼物：送了你一个礼物
                 收守护：送了你一个守护
                 收会员：送了你会员
                 */
                switch giftModel.type {
                case .gift:
                    
                    let str = lastMessage.isOutgoingMsg ? "你赠送了一个礼物" : "送了你一个礼物"
                    
                    self.lastMessage =   str.withAttributes(normal_attributed)
                    
                case .sweetGuard:
                    
                    
                    let str = lastMessage.isOutgoingMsg ? "你赠送了一个守护" : "送了你一个守护"
                    
                    self.lastMessage =   str.withAttributes(normal_attributed)
                    
                case .requireDetect:
                    
                    let str = lastMessage.isOutgoingMsg ? "你邀请了认证" : "邀请了你认证"
                    
                    self.lastMessage =   str.withAttributes(normal_attributed)
                    
                default:
                    
                    let str = lastMessage.isOutgoingMsg ? "你赠送了会员" : "送了你会员"
                    
                    self.lastMessage =   str.withAttributes(normal_attributed)
                }
                
            case _ as ST_BTIMEmojiMessageModel:
                
                let text = lastMessage.isOutgoingMsg ? "你发送了一个表情" : "你收到了一个表情"
                
                self.lastMessage =   text.withAttributes(normal_attributed)
                
                
            case let cModel as ST_Custom255AttachmentModel:
                
                /**0 :充币红包
                 1 :领取红包
                 2 :喜欢道具
                 3 :新守护 有收益
                 4 :新礼物 有收益
                 */
                switch cModel.type {
                case 1:
                    
                    let text = lastMessage.isOutgoingMsg ? "[你领取了对方的红包]" : "[对方领取了你的红包]"
                    
                    self.lastMessage =   text.withAttributes(normal_attributed)
                    
                case 2:
                    
                    self.lastMessage =   "送了你一个礼物".withAttributes(normal_attributed)
                    
                case 3:
                    
                    let text = lastMessage.isOutgoingMsg ? "[你赠送了一个守护]" : "[送了一个守护]"
                    
                    self.lastMessage =   text.withAttributes(normal_attributed)
                    
                case 4:
                    
                    
                    let text = lastMessage.isOutgoingMsg ? "[你赠送了一个礼物红包]" : "[送了你一个礼物红包]"
                    
                    self.lastMessage =   text.withAttributes(high_line_attributed)
                    
                case 5:
                    
                    let text = lastMessage.isOutgoingMsg ? "[送出了搭讪红包]" : "[对方发来搭讪红包]"
                    
                    self.lastMessage =   text.withAttributes(high_line_attributed)
                    
                default:
                    
                    let text = lastMessage.isOutgoingMsg ? "[你发送了一个红包]" : "[发送了一个红包]"
                    
                    self.lastMessage =   text.withAttributes(normal_attributed)
                    
                }
                
            case let cModel as ST_Tian270AttachmentModel:
                
                switch cModel.type {
                
                case .normalChat:
                    
                    let str = cModel.agoraType == 1 ? "语音通话" : "视频通话"
                    
                    self.lastMessage =   "[\(str)] ".withAttributes(normal_attributed)
                    
                case .busy:
                    
                    let str = lastMessage.isOutgoingMsg ? "忙线中" : "对方忙线中"
                    
                    self.lastMessage =   str.withAttributes(normal_attributed)
                    
                case .refuse:
                    
                    let str = lastMessage.isOutgoingMsg ? "已拒绝" :  "对方已拒绝"
                    
                    self.lastMessage =   str.withAttributes(normal_attributed)
                    
                default:
                    
                    let str = lastMessage.isOutgoingMsg ? "已取消" : "对方已取消"
                    
                    self.lastMessage =   str.withAttributes(normal_attributed)
                    
                }
                
            default:
                
                self.lastMessage =   "[不支持的消息类型，请更新最新版后查看]".withAttributes(normal_attributed)
            }
        default:
            
            self.lastMessage =   "[不支持的消息类型，请更新最新版后查看]".withAttributes(normal_attributed)
        }
        
        
    }
    
    
    static func decode(_ recentSession:NIMRecentSession)->ST_TianSessionSummaryStruct?{
        
        
        /// 筛查后台消息不展示在Recent Session 里面
        guard let session = recentSession.session,HIDDEN_RECENT_SESSION_SET.contains(session.sessionId) == false ,let lastMsg = recentSession.lastMessage else { return nil}
        
        let sessionID = session.sessionId
        
        
        print("NIM original Msg Session🆔 = ",sessionID,"  unread_count =",recentSession.unreadCount,"🐯🐯🐯")
        
        let tian = ST_TianSessionSummaryStruct(sessionId: sessionID, lastMessage: lastMsg, unReadCount: recentSession.unreadCount, timeString: lastMsg.timestamp.b_t_global_date_string(), date: lastMsg.timestamp)
        
        if let info = try? BINP.storage?.object(forKey: sessionID){
            
            tian.guardTop = info.guardTop
            
        }
        
        return tian
    }
}


extension ST_TianSessionSummaryStruct:Hashable{
    
    static func == (lhs: ST_TianSessionSummaryStruct, rhs: ST_TianSessionSummaryStruct) -> Bool {
        return lhs.sessionId == rhs.sessionId
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(sessionId)
        hasher.combine(timeString)
    }
}

//extension ST_TianSessionSummaryStruct:Differentiable{
//
//    var differenceIdentifier: String {
//        return sessionId
//    }
//
//    func isContentEqual(to source: ST_TianSessionSummaryStruct) -> Bool {
//        return sessionId == source.sessionId
//    }
//}





